package com.sdcqjy.property.home.fragment;

import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sdcqjy.property.R;
import com.sdcqjy.property.base.BaseFragment;
import com.sdcqjy.property.mode.AttenMode;
import com.sdcqjy.property.mode.ListMsgMode;
import com.sdcqjy.property.utils.GlideUtils;
import com.sdcqjy.property.utils.TitleUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import co.senab.photoview.PhotoView;

/**
 * Created by 磁磁帅 on 2018/3/31.
 * 功能：
 */

public class ImageFragment extends BaseFragment {
    @BindView(R.id.textTime)
    TextView textTime;
    @BindView(R.id.layout1)
    LinearLayout layout1;
    Unbinder unbinder;
    @BindView(R.id.textTitle)
    TextView textTitle;
    @BindView(R.id.textAtten)
    TextView textAtten;
    @BindView(R.id.textReadNumb)
    TextView textReadNumb;
    private ListMsgMode mode;
    private AttenMode attenMode;
    private String readNumb;

    public static ImageFragment newInstance(ListMsgMode mode) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("mode", mode);
        ImageFragment fragment = new ImageFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_image, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        mode = bundle.getParcelable("mode");

        Resources res = getResources();
        String fabushijian_ = res.getString(R.string.fabushijian_);

        textTime.setText(fabushijian_ + mode.getTime());
        String[] split = mode.imagePath.split(",");

        if (split.length == 0) return;
        ImageViewHolder imageViewHolder;
        for (int i = 0; i < split.length; i++) {
            imageViewHolder = getImageViewHolder();
            layout1.addView(imageViewHolder.icon);
            GlideUtils.displayOfUrl(getActivity(), imageViewHolder.icon, split[i]);
        }
        if (TextUtils.isEmpty(mode.title)) {
            textTitle.setVisibility(View.GONE);
        } else textTitle.setText(mode.title);

        boolean gone = TitleUtils.isGone(mode.categoryID);
        //if (gone) textAtten.setVisibility(View.GONE);
        gone = TitleUtils.has围观and关注(mode.categoryID);
        if (gone) {
            textAtten.setVisibility(View.VISIBLE);
            textReadNumb.setVisibility(View.VISIBLE);
        } else {
            textAtten.setVisibility(View.GONE);
            textReadNumb.setVisibility(View.GONE);
        }
    }

    private ImageViewHolder getImageViewHolder() {
        View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.item_img_list, null);
        return new ImageViewHolder(inflate);
    }

    public void onResume() {
        super.onResume();
        if (handler != null) {
            if (attenMode == null) handler.sendEmptyMessage(1);
            if (readNumb == null) handler.sendEmptyMessage(4);
        }
    }

    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void sendMessage(int type, Object object) {
        if (object == null) return;
        switch (type) {
            case 1:
                if (object instanceof AttenMode) {
                    attenMode = (AttenMode) object;
                    textAtten.setText(attenMode.isAttention == 1 ? R.string.yiguanzhu : R.string._guanzhu);
                }
                break;
            case 4:
                if (object instanceof String) {
                    readNumb = (String) object;
                    String s = getResources().getString(R.string.readHintStr) + readNumb;
                    textReadNumb.setText(s);
                }
                break;
        }
    }

    @OnClick(R.id.textAtten)
    public void onTextAttenClicked() {
        if (isLogin() && handler != null) {
            int isAttention = 0;
            if (attenMode != null) isAttention = attenMode.isAttention;
            handler.sendEmptyMessage(isAttention == 1 ? 3 : 2);
        }
    }

    public static class ImageViewHolder {
        @BindView(R.id.icon)
        PhotoView icon;

        ImageViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        public void setData(String mode) {
            GlideUtils.displayOfUrl(icon.getContext(), icon, mode);
        }
    }
}
